package com.jingdianjichi.oss.adapter;

import com.jingdianjichi.oss.entity.FileInfo;
import com.jingdianjichi.oss.util.MinioUtil;
import io.minio.errors.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * MinioStorageServiceImpl 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/27 21:14
 * @description
 */
public class MinioStorageAdapter implements StorageAdapter {
    @Resource
    private MinioUtil minioUtil;

    /**
     * minioUrl
     */
    @Value("${minio.url}")
    private String url;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    /**
     * 上传文件到MinIO服务器
     *
     * @param uploadFile 要上传的文件，类型为MultipartFile
     * @param bucket     存储桶名称，用于指定文件存储的位置
     * @param objectName 文件在存储桶中的对象名称，可选参数
     *
     * 该方法首先检查并创建存储桶（如果尚不存在），然后根据objectName参数决定文件的上传路径
     * 如果objectName为空，文件将直接上传至存储桶根目录；如果objectName非空，文件将上传至该目录下
     * 使用了SneakyThrows注解来处理可能抛出的异常，避免显式异常处理代码
     */
    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        // 确保目标存储桶存在
        minioUtil.createBucket(bucket);

        // 根据objectName是否为空，决定文件的上传路径
        if (objectName != null) {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + uploadFile.getOriginalFilename());
        } else {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, uploadFile.getOriginalFilename());
        }
    }

    @Override
    public List<String> geAllBucket() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return null;
    }

    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
        return minioUtil.geAllBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    public InputStream downloadFile(String bucketName, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return null;
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucket, String objectName) {
         minioUtil.deleteFile(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtil.deleteBucket(bucket);
    }



    @Override
    @SneakyThrows
    public String getUrl(String bucket, String objectName) {
        return minioUtil.getPreviewFileUrl(bucket, objectName);
    }

}
